package com.itic.mall.system.service;

import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.store.storeInfo.persistence.dao.StoreInfoDao;
import com.itic.mall.system.store.storeInfo.persistence.model.StoreInfo;
import com.itic.mall.system.store.storeInfo.persistence.model.StoreInfoExample;
import com.itic.mall.system.store.storeInfo.persistence.model.StoreInfoExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class StoreInfoService
     extends CrudService<StoreInfoDao,StoreInfo,StoreInfoExample> implements com.itic.mall.system.inter.service.StoreInfoService{
    @Autowired
    private StoreInfoDao storeInfoDao;
    
    
    public StoreInfo findStoreInfoByUserId(String userId) {
    	StoreInfoExample example = new StoreInfoExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andUserIdEqualTo(userId);
    	List<StoreInfo> storeInfos = this.storeInfoDao.selectByExample(example);
    	if(storeInfos.isEmpty())
    		return null;
    	else
    		return storeInfos.get(0);
    }
   
    public boolean editStoreInfo(StoreInfo storeInfo) {
    	String result = this.edit(storeInfo);
		return SUCCESS.equals(result)?true:false;
    }
    
    public String calTotalSale(String userId,double actualMoney) {
    	StoreInfo storeInfo = this.findStoreInfoByUserId(userId);
		double totalSaleMoney = storeInfo.getTotalSale()==null?0:storeInfo.getTotalSale();
		totalSaleMoney +=actualMoney;
		storeInfo.setTotalSale(totalSaleMoney);
		String result = this.edit(storeInfo);
    	return result;
    }
    
    public StoreInfo getByUserIdAndYunId(String userId) {
		return storeInfoDao.getByUserIdAndYunId(userId);
	}
    /***
     * 开店添加storeInfo信息
     * @param usersMall
     * @return
     */
    public String addStoreInfo(UsersMall usersMall) {
    	StoreInfo storeInfo = new StoreInfo();
    	//storeInfo.setInviteCode(inviteCode);
    	storeInfo.setNickName(usersMall.getUserNickName());
    	storeInfo.setOneLevelCount(0);
    	storeInfo.setTwoLevelCount(0);
    	storeInfo.setPreProfit(0.0);
    	storeInfo.setPreProfitCount(0);
    	storeInfo.setStoreImg(usersMall.getUserHeadImg());
    	storeInfo.setStoreName(usersMall.getUserNickName());
    	storeInfo.setTeamCount(0);
    	storeInfo.setTotalProduct(0);
    	storeInfo.setTotalProfit(0.0);
    	storeInfo.setTotalProfitCount(0);
    	storeInfo.setTotalSale(0.0);
    	storeInfo.setYunId(usersMall.getId());
    	storeInfo.setUserId(usersMall.getId());
    	String result = this.add(storeInfo);
    	return result;
    }
	

}